// 用户安全信息
let securityInfo = {
  hasTransactionPassword: true,
  transactionPassword: '123456', // 实际项目中应该是加密存储的
  email: 'user123@example.com',
  phone: '13800138000'
}

/**
 * 验证交易密码
 * @param {Object} data 密码数据
 * @returns {Object} 响应数据
 */
export function verifyTransactionPassword(data) {
  if (!securityInfo.hasTransactionPassword) {
    return {
      code: 1,
      message: '您尚未设置交易密码',
      success: false
    }
  }
  
  if (data.password === securityInfo.transactionPassword) {
    return {
      code: 0,
      message: '验证成功',
      success: true
    }
  } else {
    return {
      code: 1,
      message: '交易密码错误',
      success: false
    }
  }
}

/**
 * 设置交易密码
 * @param {Object} data 密码数据
 * @returns {Object} 响应数据
 */
export function setTransactionPassword(data) {
  if (securityInfo.hasTransactionPassword) {
    return {
      code: 1,
      message: '您已设置过交易密码，请使用修改功能',
      success: false
    }
  }
  
  if (data.password !== data.confirmPassword) {
    return {
      code: 1,
      message: '两次输入的密码不一致',
      success: false
    }
  }
  
  // 设置交易密码
  securityInfo.transactionPassword = data.password
  securityInfo.hasTransactionPassword = true
  
  return {
    code: 0,
    message: '交易密码设置成功',
    success: true
  }
}

/**
 * 修改交易密码
 * @param {Object} data 密码数据
 * @returns {Object} 响应数据
 */
export function updateTransactionPassword(data) {
  if (!securityInfo.hasTransactionPassword) {
    return {
      code: 1,
      message: '您尚未设置交易密码',
      success: false
    }
  }
  
  if (data.oldPassword !== securityInfo.transactionPassword) {
    return {
      code: 1,
      message: '原交易密码错误',
      success: false
    }
  }
  
  if (data.newPassword !== data.confirmPassword) {
    return {
      code: 1,
      message: '两次输入的新密码不一致',
      success: false
    }
  }
  
  // 修改交易密码
  securityInfo.transactionPassword = data.newPassword
  
  return {
    code: 0,
    message: '交易密码修改成功',
    success: true
  }
}

/**
 * 重置交易密码
 * @param {Object} data 密码数据
 * @returns {Object} 响应数据
 */
export function resetTransactionPassword(data) {
  if (data.newPassword !== data.confirmPassword) {
    return {
      code: 1,
      message: '两次输入的新密码不一致',
      success: false
    }
  }
  
  // 重置交易密码
  securityInfo.transactionPassword = data.newPassword
  securityInfo.hasTransactionPassword = true
  
  return {
    code: 0,
    message: '交易密码重置成功',
    success: true
  }
}

/**
 * 发送验证码
 * @param {Object} data 验证码数据
 * @returns {Object} 响应数据
 */
export function sendVerificationCode(data) {
  const { type, target } = data
  
  // 验证目标是否正确
  if (type === 'email' && target !== securityInfo.email) {
    return {
      code: 1,
      message: '邮箱不匹配',
      success: false
    }
  }
  
  if (type === 'phone' && target !== securityInfo.phone) {
    return {
      code: 1,
      message: '手机号不匹配',
      success: false
    }
  }
  
  return {
    code: 0,
    message: '验证码已发送',
    success: true
  }
}

/**
 * 验证验证码
 * @param {Object} data 验证码数据
 * @returns {Object} 响应数据
 */
export function verifyCode(data) {
  const { code } = data
  
  // 模拟验证码验证，实际项目中应该与后端验证
  if (code === '123456') {
    return {
      code: 0,
      message: '验证成功',
      success: true
    }
  } else {
    return {
      code: 1,
      message: '验证码错误',
      success: false
    }
  }
}